import { result } from 'lodash';
/*
 * @Description: 
 * @Autor: 商晓彬
 * @Date: 2021-05-24 15:13:54
 * @LastEditors: 商晓彬
 * @LastEditTime: 2022-01-04 14:29:43
 */
import { AfterViewChecked, Component, ElementRef, EventEmitter, Injector, Input, Output, ViewChild } from '@angular/core';
import { AppConsts } from '@shared/AppConsts';
import { AppComponentBase } from '@shared/common/app-component-base';
import { BopDescEditDto, BopDescServiceProxy, CreateOrUpdateBopDescInput, CommonServiceProxy, MakeTaskServiceProxy } from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import { IOrganizationUnitsTreeComponentData, OrganizationUnitsTreeComponent } from '../shared/organization-unit-tree.component';
import * as _ from 'lodash';
import { finalize } from 'rxjs/operators';
import { MessageService } from '@abp/message/message.service';
@Component({
    selector: 'createOrEditBopTaskModal',
    templateUrl: './create-or-edit-boptask-modal.component.html',
    styleUrls: ['./create-or-edit-boptask-modal.component.less']
})
export class CreateOrEditBopTaskModalComponent extends AppComponentBase {
    //引入子组件
    @ViewChild('createOrEditModal') modal: ModalDirective;

    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
    @Input() editData: any;  //要修改的数据
    @Input() title: string;  //标题
    workingHourOption = [
        {
            label: '请选择',
            value: undefined
        }, {
            label: '工步',
            value: '1'
        }, {
            label: '工序',
            value: '0'
        }
    ];
    saving = false;
    active = false;
    model = { label: undefined, value: undefined };
    lotNo = { label: undefined, value: undefined };
    modelList = [];
    lotNoList = [];
    form: BopDescEditDto = new BopDescEditDto();
    saveFlag = true;
    constructor(
        injector: Injector,
        private _bopDescServiceProxy: BopDescServiceProxy,
        private _messageService: MessageService,
        private _commonServiceProxy: CommonServiceProxy,
        private _makeTaskServiceProxy: MakeTaskServiceProxy,
    ) {
        super(injector);
    }
    getName(event) {
        this._makeTaskServiceProxy.getJobNumber(
            this.lotNo.value,
            this.model.value
        )
            .pipe(finalize(() => { })).subscribe(result => {
                this.form.modelName = result.productName;
                this.form.drawingNo = result.drawingNo;
            })
    }
    //保存BOP任务
    save(): void {
        //获取实体
        let input = new CreateOrUpdateBopDescInput();
        //将form表单赋值给input的bopdesc下
        input.bopDesc = this.form;
        input.bopDesc.model = this.model.label;
        input.bopDesc.modelId = this.model.value;
        input.bopDesc.lotNo = this.lotNo.label;
        input.bopDesc.lotNoId = this.lotNo.value;
        this.saving = true;
        if (this.saveFlag) {
            this.saveFlag = false;
            this._bopDescServiceProxy.createOrUpdateBopDesc(input)
                .pipe(finalize(() => { this.saving = false; }))
                .subscribe((result) => {
                    this.saveFlag = true;
                    if (result.code == 0) {
                        this._messageService.success('保存成功');
                        this.close();
                        this.modalSave.emit(null);
                    } else {
                        this._messageService.error(result.message)
                    }
                });
        }

    }
    //打开弹窗
    onShown(): void {
        // document.getElementById('model').focus();
        //型号
        this._commonServiceProxy.getModelList(
        )
            .pipe(finalize(() => { })).subscribe(result => {
                this.modelList = result;
            })
    }
    onChange(event) {
        //发次
        this._commonServiceProxy.getLotNoList(
            this.model.value
        )
            .pipe(finalize(() => { })).subscribe(result => {
                this.lotNoList = result;
            })
    }
    //查看新增任务
    show(): void {
        if (this.title == '修改任务') {
            this.initEditInfo()
        } else {
            this.form = new BopDescEditDto();
        }
        this.active = true;
        this.modal.show();

    }
    //初始化修改数据
    initEditInfo() {
        this.form.id = this.editData.id;
        this.model.label = this.editData.model;
        this.model.value = this.editData.modelId;
        this._commonServiceProxy.getLotNoList(
            this.model.value
        )
            .pipe(finalize(() => { })).subscribe(result => {
                this.lotNoList = result;
                this.lotNo.label = this.editData.lotNo;
                this.lotNo.value = this.editData.lotNoId;
            })
        this.form.modelName = this.editData.modelName;
        this.form.drawingNo = this.editData.drawingNo;
        this.form.stageSign = this.editData.stageSign;
        this.form.workingHourStatus = this.editData.workingHourStatus;
    }
    //关闭事件
    close(): void {
        //清空form表单
        this.form = new BopDescEditDto();
        this.model.label = undefined;
        this.model.value = undefined;
        this.lotNo.label = undefined;
        this.lotNo.value = undefined;
        this.active = false;
        this.modal.hide();
    }
}
